package com.marlon.springboot.jap.controller;

import com.marlon.springboot.jap.entity.User;
import com.marlon.springboot.jap.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author marlon
 * @create 2023-11-18 14:20
 */
@Controller
public class UserController {

    @Resource
    IUserService userService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<User> users = userService.queryAll();
        model.addAttribute("users", users);
        return "user/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "user/add";
    }

    @RequestMapping("/add")
    public String add(User user) {
        userService.save(user);
        return "redirect:/list";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Model model, String id) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "user/edit";
    }

    @RequestMapping("/edit")
    public String edit(User user) {
        userService.updateById(user);
        return "redirect:/list";
    }


    @RequestMapping("/delete")
    public String delete(String id) {
        userService.deleteById(id);
        return "redirect:/list";
    }
}
